package com.nmm.demo.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nmm.demo.data.DbData;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;

/**
 * AI工具函数提供服务
 * @author niemingming
 * @Date 2025/6/13
 */
@Service
public class McpBusinessService {

  // 初始化数据项
  private JSONObject data = JSON.parseObject(DbData.data);


  @Tool(name = "selectUserByNo", description = "根据用户工号、姓名，模糊查询用户信息")
  public JSONArray selectUserByNo(@ToolParam(description = "用户工号、姓名") String userNo) {
    JSONArray res = new JSONArray();
    System.out.println("查询用户：" + userNo);
    for (int i = 0; i < data.getJSONArray("users").size(); i++) {
      JSONObject user = data.getJSONArray("users").getJSONObject(i);
      if (user.getString("userNo").contains(userNo) || user.getString("name").contains(userNo)) {
        res.add(user);
      }
    }
    return res;
  }
  @Tool(name = "selectOrderByNo", description = "根据订单编号，模糊查询订单信息")
  public JSONArray selectOrderByNo(@ToolParam(description = "订单编号") String orderNo) {
    System.out.println("查询订单：" + orderNo);
    JSONArray res = new JSONArray();
    for (int i = 0; i < data.getJSONArray("orders").size(); i++) {
      JSONObject order = data.getJSONArray("orders").getJSONObject(i);
      if (order.getString("orderNo").contains(orderNo) ) {
        res.add(order);
      }
    }
    return res;
  }


}
